package com.polaris.live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.databinding.AdapterHonorWallBinding
import com.polaris.live.databinding.AdapterHonorWallNumBinding
import com.polaris.live.resp.back_resp.UserMedalVo
import com.polaris.live.resp.bean.GameHonorNumBean
import com.polaris.live.utils.file.FileUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder
import org.libpag.PAGFile
import java.io.File

/**
 * HonorWallGameAdapter
 *
 * @author Created by 半仙 on 2024/4/20/020 17:35
 */
class HonorWallGameAdapter : BaseAdapter<Any, BaseViewHolder>(0) {
    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is GameHonorNumBean -> TYPE_NUM
            is UserMedalVo -> TYPE_HONOR
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_NUM -> HonorWallNumViewHolder(
                AdapterHonorWallNumBinding.inflate(layoutInflater, parent, false)
            )

            TYPE_HONOR -> HonorWallViewHolder(
                AdapterHonorWallBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is HonorWallNumViewHolder -> if (item is GameHonorNumBean) holder.bind(item)
            is HonorWallViewHolder -> if (item is UserMedalVo) holder.bind(item)
        }
    }

    class HonorWallNumViewHolder(
        binding: AdapterHonorWallNumBinding,
    ) : BaseBindingViewHolder<AdapterHonorWallNumBinding, GameHonorNumBean>(binding) {
        override fun bind(item: GameHonorNumBean) {
            mBinding.apply {
                itemHonorWallNum.text = "${item.name}(${item.hasCount}/${item.count})"
            }
        }
    }

    class HonorWallViewHolder(
        binding: AdapterHonorWallBinding,
    ) : BaseBindingViewHolder<AdapterHonorWallBinding, UserMedalVo>(binding) {
        override fun bind(item: UserMedalVo) {
            mBinding.apply {
                ivHonorItem.visibility = View.GONE
                pagHonorItem.visibility = View.GONE
                if (item.createTime != null) {
                    llItemCenter.alpha = 1f
                    if (item.pagUrl?.isNotEmpty() == true) {
                        val path = FileUtils.getFilePath(FileUtils.getPagFile(), item.pagUrl)
                        if (File(path).exists()) {
                            pagHonorItem.visibility = View.VISIBLE
                            pagHonorItem.apply {
                                setRepeatCount(-1)
                                composition = PAGFile.Load(path)
                                play()
                            }
                        } else {
                            showImage(mBinding, item)
                        }
                    } else {
                        ivHonorItem.visibility = View.VISIBLE
                        Glide.with(ivHonorItem.context).load(ImageUtils.imgPath(item.photoUrl))
                            .into(ivHonorItem)
                    }
                } else {
                    showImage(mBinding, item)
                }
                tvHonorItem.text = item.name
            }
        }

        private fun showImage(mBinding: AdapterHonorWallBinding, item: UserMedalVo) {
            mBinding.apply {
                ivHonorItem.visibility = View.VISIBLE
                llItemCenter.alpha = 0.4f
                Glide.with(ivHonorItem.context).load(ImageUtils.imgPath(item.photoUrl))
                    .into(ivHonorItem)
            }
        }

    }

    companion object {

        const val TYPE_NUM = 0
        const val TYPE_HONOR = 1
    }
}